One place for hosting & domains

      Install

      How To Install the Windows Subsystem for Linux 2 on Microsoft Windows 10


      Introduction

      The Windows Subsystem for Linux 2, WSL 2 or WSL for short, is a tool on Microsoft Windows 10 that allows developers to run a Linux environment directly on Windows without any modifications, secondary Virtual Machine software, or dual-boot setups. The WSL natively integrates with most applications on your workstation, allowing for a Linux-like development experience on Windows. The WSL accomplishes this by using Microsoft’s built in virtualization software called Hyper-V to run.

      In this tutorial you’ll enable the WSL, install Ubuntu 20.04 onto your workstation using the WSL, and install Microsoft’s new Windows Command Line to access your Ubuntu 20.04 installation. This will provide you with a Linux programming environment that is native to Windows.

      Prerequisites

      In order to follow along with this guide, you’ll need:

      • Personal Computer with Windows 10 installed: The Windows Subsystem for
        Linux 2 requires Windows 10 version 1903 or higher with build 18362 or higher. For ARM64 systems, version 2004 or higher with build 19041 is required. Builds lower than this will not support the WSL 2.

      Warning: If you are installing the WSL on a virtual machine you will need to expose CPU virtualizations flags to the virtual machine. For example, if you are installing the WSL on a virtualized Windows 10 that is running in Hyper-V named MyWSL you will need to run the following command on the virtualization host, not the virtual machine. This is a Windows command so you will need an elevated PowerShell prompt to do this. Replace MyWSL with the name of your VM in Hyper-V:

      • Set-VMProcessor -VMName MyWSL -ExposeVirtualizationExtensions $true

      If you are not installing the WSL on a virtual machine, you can skip this warning entirely.

      Step 1 — Enabling Windows Services for the WSL

      The first thing you need to do is enable specific Windows services so that you can run the WSL. These services come with Windows but are turned off by default until you decide you need them. Open up the Start menu and search for PowerShell. You’ll need to right click on PowerShell and click on Run as Administrator.

      Open Windows PowerShell as an administrator

      Once you’ve done this, a PowerShell window will open. You’ll use the Windows Deployment Image Servicing and Management tool to enable optional Windows features that are disabled by default. Run the following command to enable the WSL feature:

      PS C:Windowssystem32> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
      

      Once you run the command you’ll see output similar to this:

      output

      Deployment Image Servicing and Management tool Version: 10.0.19041.844 Image Version: 10.0.19042.985 Enabling feature(s) [==========================100.0%==========================] The operation completed successfully.

      Next, run the following command in PowerShell to enable Windows’ Virtual Machine Platform. This enables the second generation of the WSL by enabling Hyper-V and allowing Windows’ to install Linux using it.

      PS C:Windowssystem32> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
      

      Once you run the command you’ll see output similar to this:

      output

      Deployment Image Servicing and Management tool Version: 10.0.19041.844 Image Version: 10.0.19042.985 Enabling feature(s) [==========================100.0%==========================] The operation completed successfully.

      Once you have done this you will need to restart Windows for these changes to take place.

      After the restart is finished, log back in to your workstation.

      Next you’ll need to download the latest Linux kernel update package from Microsoft and install it.

      To install this package, click to download the wsl_update_x64.msi package to your local workstation. Once it is downloaded, run the application and follow the prompts to install it.

      Download the Linux Kernel patch and run

      Finally, you need to set the default version of the WSL to version 2. Open another PowerShell terminal as an administrator and run the following command:

      PS C:Windowssystem32> wsl --set-default-version 2
      

      Once you run the command you’ll see output similar to this:

      output

      For information on key differences with WSL 2 please visit https://aka.ms/wsl2

      Now that you’ve done this the WSL 2 is enabled and ready to run on your workstation. Next you’ll need to install a Linux-based operating system using the WSL 2.

      Step 2 — Installing Ubuntu 20.04 using the WSL

      Now that you have the WSL 2 installed, you next need to install a Linux distribution from the Microsoft Store. For this tutorial we will be installing the default Ubuntu option from the store, which happens to be Ubuntu 20.04 at this time. There are many different options to choose from and all should take a nearly identical approach to installation as laid out in this tutorial.

      Open your Start menu, search for the Microsoft Store, and open it.

      Open the Microsoft Store

      Locate the search bar in the upper right-hand corner and search for Linux.

      Search for Linux

      Many options will appear; select the Ubuntu tile to install the latest supported version of Ubuntu. Click the Get button to add the app to your account. Once you’ve done this, the Get button will be replaced by the Install button. Click that button to install Ubuntu to your local workstation. Once the installation is done a Launch button will appear. Click this to launch Ubuntu.

      Select Ubuntu and Install

      The first time you launch Ubuntu it will inform you that it is performing initial configuration that may take a few minutes. After this it will prompt you for a Linux system username and password. This username and password doesn’t have to be the same as your system but it will be required for you to be able to use sudo to gain admin privileges.

      Perform Initial Setup of Ubuntu

      Once you have created your user the initial setup is complete and Ubuntu is ready to be used.

      Once initial setup is done Ubuntu is ready to be used

      Now that you have Ubuntu installed, you’ll install a more robust terminal for accessing Ubuntu through Windows.

      Step 3 — Installing and Configuring the New Windows Command Line

      Ubuntu provides a default terminal for use, but Microsoft open sourced and reimplemented their Terminal tool and branded it at Windows Terminal. This terminal supports many more customization and ease-of-use options than the default Ubuntu terminal, so you’ll want to install it.

      Next you’ll install this terminal on your workstation. Navigate back to the search bar in the Microsoft Store and search for Terminal.

      Search Terminal in the Microsoft Store looking for Windows Terminal

      Install the Windows Terminal the same way you installed Ubuntu, by clicking Get and Install. Launch the terminal by clicking Launch or selecting the program from your Start menu.

      Get and Install the Windows Terminal. Then click launch

      By default, the Windows Terminal opens up a PowerShell console.

      Default Windows Terminal opens up PowerShell

      If you want to open a different console, click the down arrow button to see what is available. You have access to PowerShell, Command Prompt, and Azure Cloud Shell by default. When you install any WSL Linux, it will appear here as well like the Ubuntu install did. Clicking Ubuntu will open an Ubuntu shell in a new tab.

      Windows Terminal supports many different options

      If you want to change some of the Terminal’s default options, click the down arrow button and select Settings.

      Open up settings to change the default to the WSL Ubuntu

      Next, set Ubuntu as your default console. Under Default Profile select your Ubuntu WSL image, and click Save to make it your default option.

      Select Ubuntu as your Default Profile

      Now when you click the + button or open a new terminal Ubuntu will be the default shell.

      Clicking the plus or opening the terminal new will give you your WSL Ubuntu shell

      You now have the new Windows Command Line tool installed and configured to open your WSL Ubuntu terminal by default. You are now ready to use Linux on Windows using the WSL.

      Conclusion

      You now have a fully functioning Linux environment running in Windows. You configured your computer to take advantage of the Windows Subystem for Linux, and installed an Ubuntu environment using the WSL. If you prefer a different Linux distribution, there are others you can install, including Debian, SUSE, or Kali Linux. From here, you can install developer tools to have a complete Linux development environment running on Windows.



      Source link

      How To Install and Manage System Packages in Ansible Playbooks



      Part of the Series:
      How To Write Ansible Playbooks

      Ansible is a modern configuration management tool that doesn’t require the use of an agent software on remote nodes, using only SSH and Python to communicate and execute commands on managed servers. This series will walk you through the main Ansible features that you can use to write playbooks for server automation. At the end, we’ll see a practical example of how to create a playbook to automate setting up a remote Nginx web server and deploy a static HTML website to it.

      Automating the installation of required system packages is a common operational task in Ansible playbooks, since a typical application stack requires software from different sources.

      The apt module manages system packages on Debian-based operating systems such as Ubuntu, the distribution we’re using on remote nodes throughout this guide. The following playbook will update the apt cache and then make sure Vim is installed on remote nodes.

      Create a new file called playbook-09.yml in your ansible-practice directory:

      • nano ~/ansible-practice/playbook-09.yml

      Then add the following lines to the new playbook file:

      ~/ansible-practice/playbook-09.yml

      ---
      - hosts: all
        become: yes
        tasks:
          - name: Update apt cache and make sure Vim is installed
            apt:
              name: vim
              update_cache: yes
      

      Save and close the file when you’re done.

      Notice that we’ve included the become directive in the beginning of the play. This is required since installing packages requires administrative system permissions.

      Removing a package is done in a similar way, the only change is that you have to define the package state to absent. The state directive has a default value of present, which will make sure that the package is installed on the system, regardless of the version. The package will be installed if not present. To assure you have the latest version of a package, you can use latest instead. This will cause apt to update the requested package if that is not on their latest version.

      Remember to provide the -K option when running this playbook, since it requires sudo permissions:

      • ansible-playbook -i inventory playbook-09.yml -u sammy -K

      Output

      BECOME password: PLAY [all] ********************************************************************************************** TASK [Gathering Facts] ********************************************************************************** ok: [203.0.113.10] TASK [Update apt cache and make sure Vim is installed] ************************************************** ok: [203.0.113.10] PLAY RECAP ********************************************************************************************** 203.0.113.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

      When installing multiple packages, you can use a loop and provide an array containing the names of the packages you want to install. The following playbook will make sure the packages vim, unzip, and curl are installed and in their latest version.

      Create a new file called playbook-10.yml in your ansible-practice directory, on your Ansible control node:

      • nano ~/ansible-practice/playbook-10.yml

      Add the following content to the new playbook file:

      ~/ansible-practice/playbook-10.yml

      ---
      - hosts: all
        become: yes
        tasks:
          - name: Update apt cache and make sure Vim, Curl and Unzip are installed
            apt:
              name: "{{ item }}"
              update_cache: yes
            loop:
              - vim
              - curl
              - unzip
      

      Save and close the file when you have finished.

      Then, run ansible-playbook with the same connection arguments from the previous examples, and don’t forget to include the -K option since this playbook requires administrative privileges:

      • ansible-playbook -i inventory playbook-09.yml -u sammy -K

      You’ll see output like this, indicating that the same task run through three iterations using the different values we have provided: vim, curl, and unzip:

      Output

      BECOME password: PLAY [all] *************************************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************************** ok: [203.0.113.10] TASK [Update apt cache and make sure Vim, Curl and Unzip are installed] ************************************************************************** ok: [203.0.113.10] => (item=vim) ok: [203.0.113.10] => (item=curl) changed: [203.0.113.10] => (item=unzip) PLAY RECAP *************************************************************************************************************************************** 203.0.113.10 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

      For more details on how to manage system packages, including how to remove packages and how to use advanced apt options, you can refer to the official documentation.



      Source link

      How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 18.04


      The author selected Open Sourcing Mental Illness Ltd to receive a donation as part of the Write for DOnations program.

      Introduction

      PHP is a popular server scripting language known for creating dynamic and interactive web pages. Getting up and running with your language of choice is the first step in learning to program.

      This tutorial will guide you through installing PHP 7.4 on Ubuntu and setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.

      Prerequisites

      To complete this tutorial, you will need a local or virtual machine with Ubuntu 18.04 installed and have administrative access and an internet connection to that machine. You can download this operating system via the Ubuntu releases page.

      Step 1 — Setting Up PHP 7.4

      You’ll be completing your installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

      The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. The article An Introduction to the Linux Terminal can get you better oriented with the terminal.

      On Ubuntu, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left-hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

      Ubuntu terminal

      You will want to avoid relying on the default version of PHP because that default version could change depending on where you are running your code. You may also wish to install a different version to match an application you are using or to upgrade to a newer version, such as PHP 8.

      Run the following command to update apt-get itself, which ensures that you have access to the latest versions of anything you want to install:

      Next, install software-properties-common, which adds management for additional software sources:

      • sudo apt -y install software-properties-common

      The -y flag will automatically agree to the installation. Without that, you would receive a prompt in your terminal window for each installation.

      Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:

      • sudo add-apt-repository ppa:ondrej/php

      Finally, you update apt-get again so your package manager can see the newly listed packages:

      Now you’re ready to install PHP 7.4 using the following command:

      • sudo apt -y install php7.4

      Check the version installed:

      You will receive something similar to the following:

      Output

      PHP 7.4.0beta4 (cli) (built: Aug 28 2019 11:41:49) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies with Zend OPcache v7.4.0beta4, Copyright (c), by Zend Technologies

      Besides PHP itself, you will likely want to install some additional PHP modules. You can use this command to install additional modules, replacing PACKAGE_NAME with the package you wish to install:

      • sudo apt-get install php7.4-PACKAGE_NAME

      You can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want to install:

      • sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath

      This command will install the following modules:

      • php7.4-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
      • php7.4-json - for working with JSON data
      • php7.4-common - documentation, examples, and common modules for PHP
      • php7.4-mysql - for working with MySQL databases
      • php7.4-zip - for working with compressed files
      • php7.4-gd - for working with images
      • php7.4-mbstring - used to manage non-ASCII strings
      • php7.4-curl - lets you make HTTP requests in PHP
      • php7.4-xml - for working with XML data
      • php7.4-bcmath - used when working with precision floats

      PHP configurations related to Apache are stored in /etc/php/7.4/apache2/php.ini. You can list all loaded PHP modules with the following command:

      You have installed PHP and verified the version you have running. You also installed any required PHP modules and were able to list the modules that you have loaded.

      You could start using PHP right now, but you will likely want to use various libraries to build PHP applications quickly. Before you test your PHP environment, first set up a dependency manager for your projects.

      Step 2 — Setting Up Composer for Dependency Management (Optional)

      Libraries are a collection of code that can help you solve common problems without needing to write everything yourself. Since there are many libraries available, using a dependency manager will help you manage multiple libraries as you become more experienced in writing PHP.

      Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and will manage installing and updating these packages.

      Although similar, Composer is not a package manager in the same sense as yum or apt. It deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does, however, support a global project for convenience via the global command.

      This idea is not new, and Composer is strongly inspired by Node’s npm and Ruby’s bundler.

      Suppose:

      • You have a project that depends on several libraries.
      • Some of those libraries depend on other libraries.

      Composer:

      • Enables you to declare the libraries you depend on.
      • Finds out which versions of which packages can and need to be installed and installs them by downloading them into your project.
      • Enables you to update all your dependencies in one command.
      • Enables you to see the Basic Usage chapter for more details on declaring dependencies.

      There are, in short, two ways to install Composer: locally as part of your project or globally as a system-wide executable. Either way, you will start with the local install.

      Locally

      To quickly install Composer in the current directory, run this script in your terminal:

      • php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
      • php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
      • php composer-setup.php
      • php -r "unlink('composer-setup.php');"

      This installer script will check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The four lines will, in order:

      • Download the installer to the current directory
      • Verify the installer SHA-384, which you can also cross-check here
      • Run the installer
      • Remove the installer

      The installer will check a few PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP that can be run on the command line, amongst other things.

      In order to run Composer, you use php composer.phar. As an example, run this command to see the version of Composer you have installed:

      • php composer.phar --version

      To use Composer locally, you will want your composer.phar file to be in your project’s root directory. You can start in your project directory before installing Composer. You can also move the file after installation. You can also install Composer to a specific directory by using the --install-dir option and additionally (re)name it using the --filename option.

      Since Composer is something used across projects, it’s recommended that you continue to the next portion and set Composer to run globally.

      Globally

      You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your $PATH, you can access it globally. You can even make it executable on Ubuntu (and other Unix systems) and invoke it without directly using the PHP interpreter.

      After installing locally, run this command to move composer.phar to a directory that is in your path:

      • sudo mv composer.phar /usr/local/bin/composer

      If you’d like to install it only for your user and avoid requiring root permissions, you can use ~/.local/bin instead, which is available by default on some Linux distributions:

      • mv composer.phar ~/.local/bin/composer

      Now to run Composer, use composer instead of php composer.phar. To check for your Composer version, run:

      As a final step, you may optionally initialize your project with composer init. This will create the composer.json file that will manage your project dependencies. Initializing the project will also let you define project details such as Author and License, and use Composer’s autoload functionality. You can define dependencies now or add them later.

      Run this command to initialize a project:

      Running this command will start the setup wizard. Details that you enter in the wizard can be updated later, so feel free to leave the defaults and just press ENTER. If you aren’t ready to install any dependencies, you can choose no. Enter in your details at each prompt:

      Output

      This command will guide you through creating your composer.json config. Package name (sammy/php_install): sammy/project1 Description []: Author [Sammy <sammy@digitalocean.com>, n to skip]: Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: project License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to define your dev dependencies (require-dev) interactively [yes]? no { "name": "sammy/project1", "type": "project", "authors": [ { "name": "Sammy", "email": "sammy@digitalocean.com" } ], "require": {} } Do you confirm generation [yes]? yes

      Before you confirm the generation, you will see a sample of the composer.json file that the wizard will create. If it all looks good, you can confirm the default of yes. If you need to start over, choose no.

      The first time you define any dependency, Composer will create a vendor folder. All dependencies install into this vendor folder. Composer also creates a composer.lock file. This file specifies the exact version of each dependency and subdependency used in your project. This assures that any machine on which your program is run, will be using the exact same version of each packages.

      Note: The vendor folder should never be committed to your version control system (VCS). The vendor folder only contains packages you have installed from other vendors. Those individual vendors will maintain their own code in their own version control systems. You should only be tracking the code you write. Instead of committing the vendor folder, you only need to commit your composer.json and composer.lock files. You can learn more about ignoring specific files in How To Use Git: A Reference Guide.

      Now that you have PHP installed and a way to manage your project dependencies using Composer, you’re ready to test your environment.

      Step 3 — Testing the PHP Environment

      To test that your system is configured correctly for PHP, you can create and run a basic PHP script. Call this script hello.php:

      This will open a blank file. Put the following text, which is valid PHP code, inside the file:

      hello.php

      <?php
      echo 'Hello World!';
      ?>
      

      Once you’ve added the text, save and close the file. You can do this by holding down the CTRL key and pressing the x key. Then choose y and press ENTER.

      Now you can test to make sure that PHP processes your script correctly. Type php to tell PHP to process the file, followed by the name of the file:

      If the PHP is processed properly, you will see only the characters within the quotes:

      Output

      Hello World!

      PHP has successfully processed the script, meaning that your PHP environment is successfully installed and you’re ready to continue your programming journey.

      Conclusion

      At this point, you have a PHP 7.4 programming environment set up on your local Ubuntu machine and can begin a coding project.

      Before you start coding, you may want to set up an Integrated Development Environment (IDE). While there are many IDEs to choose from, VS Code is a popular choice as it offers many powerful features such as a graphical interface, syntax highlighting, and debugging.

      With your local machine ready for software development, you can continue to learn more about coding in PHP by following How To Work With Strings in PHP.



      Source link