Install Node.js with npm on Ubuntu

In this tutorial, we are going to learn how to install Node.js with npm on Ubuntu. Node.js is the opensource JavaScript Run-time environment for server-side execution of JavaScript code. Node.js built on Chrome’s V8 JavaScript engine so it can be used to build different types of server-side application.

Where npm stands for Node Package Manager which is the default package manager for Node.js. npm is the worlds largest software registry for Node.js packages with thousands of packages available.

In this tutorial we will install Node.js in following two ways:

  1. Install Node.js and npm using Ubuntu repository
  2. Install Node.js and npm using nvm
  3. Install Node.js from NodeSource repository.

1. Install Node.js and npm using Ubuntu repository

First, Update Ubuntu apt package manager index by running following command.

sudo apt update

Install Node.js from Ubuntu global repository by typing

sudo apt install node

Confirm the installation of Node.js by typing

node --version
How to install Node.js with npm on Ubuntu - Check Node.js Version
How to install Node.js with npm on Ubuntu – Check Node.js Version

Install npm by running following command

sudo apt install npm

Confirm the installation of npm by typing

npm --version
How to install Node.js with npm on Ubuntu - Check npm Version
How to install Node.js with npm on Ubuntu – Check npm Version

 

2. Install Node.js and npm using nvm

NVM stands for Node Version Manager which is used to manage multiple Node.js versions. If you want to install or uninstall different versions of Node.js then NVM is there for you.

First, we will install NVM (Node Package Manager) on your system. So download NVM installation script running the following command.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
How to install Node.js and npm using nvm on Ubuntu 18.04
How to install Node.js and npm using nvm on Ubuntu 18.04

Check nvm version and confirm installation typing

nvm --version

Now install Node.js by using the following command.

nvm install node

Verify Node.js installation by typing

node --version

The output should be:

Output
v10.14.0

You can install multiple versions of Node.js. To do so type following:

nvm install 8.14
nvm install --lts
nvm install 11.3

To list all the versions installed run following command.

nvm ls

You can change the current default version of Node.js by using the following command.

nvm use 8.14

To uninstall a Node.js version type following command

nvm uninstall 11.14

Install Node.js from NodeSource Repository

NodeSource company provides enterprise grade node support also maintains repository containing latest version of Node.js.

To enable NodeSource repository on your system run following command.

curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -

NOTE : The latest LTS version of Node.js is 10.x if you want to install 8.x version then just replace setup_10.x with setup_8.x

Now install Node.js and npm package typing.

sudo apt install nodejs

Verify installtion of Node.js and npm running following command

node --version
npm --version

Install Development Tools

Now install some packages needed for development by running following command

sudo apt install gcc g++ make

Uninstall Node.js and npm

Uninstall Node.js use following command

sudo apt remove nodejs npm
sudo apt autoremove

To uninstall node.js version using nvm type following command

nvm uninstall 10.14

Conclusion

You have successfully learned how to install Node.js with npm on Ubuntu 18.04. If you have any queries dont forget to comment below.

Install Let’s Encrypt on Ubuntu

By using Let’s Encrypt you can get a free valid SSL certificate. Let’s Encrypt is the Certificate Authority (CA) which provides free SSL certificate. To get SSL certificate Certbot client is used which fetches and deploys SSL certificate on your server. In this tutorial, you are going to learn how to install Let’s Encrypt SSL on Ubuntu 18.04.

Before we begin

Let’s Encrypt certificate can only be requested from the server the domain is pointing to. Let’s Encrypt checks if the domain is pointed to the current server and if successful, it issues the certificate.

Prerequisites

1. Before you start to install Let’s Encrypt SSL on Ubuntu 18.04 using the Certbot client. You must have the non-root user account on your server with sudo privileges.

2. Make it sure your domain is pointing to the current server.

1. Install Certbot Client

To install Certbot client you need to add PPA on the server then you will need to update the package manager index. After that, you will install the Certbot client.

Add PPA by typing following in the terminal

sudo add-apt-repository ppa:certbot/certbot

Update package manager index by typing following

sudo apt update

Now install Certbot client by executing following command

sudo apt install python-certbot-nginx

Confirm the installation by typing

certbot --version

2. Setting Up Firewall

If you have enabled UFW firewall then you need to adjust the settings to allow HTTPS traffic.

To check current status type following command

sudo ufw status
Output
Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Now to get HTTPS traffic in, you should add ‘WWW Full’ rule and delete ‘WWW’ rule which will become redundant.

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

Now the status should be:

Output
Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx Full                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx Full (v6)            ALLOW       Anywhere (v6)

3. Setting up Let’s Encrypt SSL on Nginx

Let’s Encrypt do a strong domain validation for ownership of the domain. After successful verification, it issues the certificate. In below command replace example with your domain name

sudo certbot --apache -d example.com -d www.example.com

If you are first time installing certificate then Certbot will ask you to enter Email ID and agree terms and conditions.

After above step Certbot will ask you to configure HTTPS settings.

Output
Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter](press 'c' to cancel):

Select your choice and continue to next step. We recommend to choose Redirect Option if you dont want to modify configuration file manually.

5. Autorenewal For SSL Certificates

All of these Let’s Encrypt certificates are short-lived and expires after 90 days. So you will have to update these certificates before they expire by running the following command.

sudo certbot renew

You can automate this process by adding a cronjob. Enter the following command to open crontab

sudo crontab -e

Add following lines to end of the file. It will run the command twice a day and renews if the certificate is about to expire.

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

4. Installing Let’s Encrypt Wildcard Certificates

Let’s Encrypt now supports wildcard certificate using new ACME2 protocol. By using wildcard certificate \*.example.com like this, you can use one certificate for multiple sub-domains like site1.example.com, site2.example.com, site3.example.com etc. To install the wildcard certificate type following command.

sudo certbot certonly --manual -d *.example.com --agree-tos --no-bootstrap --manual-public-ip-logging-ok --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

NOTE: Now you will see the message at center in output to add TXT record. So make DNS changes to your domain and add specific TXT record with given value inside output of above command

Conclusion

You have learned how to install Let’s Encrypt SSL on Ubuntu 18.04 by using certbot. If you have any queries regarding this please don’t forget to comment below.

Check Ubuntu Version

Every 6 months Ubuntu’s new version released and every 2 years we get LTS version of Ubuntu. It very important to check the Ubuntu Version you are using if you want to learn something reading tutorials. In this tutorial, you are going to learn how to check Ubuntu Version.

Check Ubuntu Version With Command Line

Checking Ubuntu Version from the command line is the best way to check Ubuntu Version as it does not depend on the desktop environment you are using.

Here we will check Ubuntu Version by using lsb_release (Linux Standard Base) utility. To check Ubuntu Version type following command. This method works always it doesn’t matter which desktop environment you are using.

lsb_release -a

The output should be similar like below:

Output
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.1 LTS
Release:	18.04
Codename:	bionic

If you want to print only distribution name then type the following command.

lsb_release -d

The output should be similar like below:

Output
Description:	Ubuntu 18.04.1 LTS

Check Ubuntu Version using /etc/os-release file

os-release file contains identification data of operating system. If you are running Ubuntu distro 16.04 or newer then you can use following command.

cat /etc/os-release

The output should be similar like below:

Output
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Check Ubuntu Version using /etc/issue file

You can also use /etc/issue file which contains identification data of oprating system to recoganise Ubuntu Version. To check Ubuntu Version type:

cat /etc/issue

The output should be similar like this:

Output
Ubuntu 18.04.1 LTS \n \l

Check System Info using hostnamectl command

hostnamectl command is used to set hostname in Ubuntu but we can also check OS information using this command.

hostnamectl

The output should be similar to this:

   Static hostname: linux4one
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 2381ec6bc94945e781c843f399507ea2
           Boot ID: dafec777a4304a56a98047b1a88b57d0
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-38-generic
      Architecture: x86-64

Check System Info using Neofetch

Neofetch is a command line utility used to check essential system information. Neofetch shows system information beautifully.

Update the package manager index by typing:

sudo apt update

Install Neofetch command line utility using the following command

sudo apt install neofetch

Now after installation completes you can check system information by using the following command.

neofetch

The output should be similar to this:

Output
            
            .-/+oossssoo+/-.               [email protected] 
        `:+ssssssssssssssssss+:`           --------------- 
      -+ssssssssssssssssssyyssss+-         OS: Ubuntu 18.04.1 LTS x86_64 
    .ossssssssssssssssssdMMMNysssso.       Host: 300E4C/300E5C/300E7C 0.1 
   /ssssssssssshdmmNNmmyNMMMMhssssss/      Kernel: 4.15.0-38-generic 
  +ssssssssshmydMMMMMMMNddddyssssssss+     Uptime: 2 days, 7 hours, 19 mins 
 /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Packages: 2015 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Shell: bash 4.4.12 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Resolution: 1366x768 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   DE: GNOME 3.28.3 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM: GNOME Shell 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   WM Theme: Adwaita 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Theme: Ambiance [GTK2/3] 
 /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/    Icons: Ubuntu-mono-dark [GTK2/3] 
  +sssssssssdmydMMMMMMMMddddyssssssss+     Terminal: gnome-terminal 
   /ssssssssssshdmNNNNmyNMMMMhssssss/      CPU: Intel i3-3210M (4) @ 3.100GHz 
    .ossssssssssssssssssdMMMNysssso.       GPU: NVIDIA GeForce 610M 
      -+sssssssssssssssssyyyssss+-         GPU: Intel 3rd Gen Core processor Graph 
        `:+ssssssssssssssssss+:`           Memory: 4276MiB / 7758MiB 
            .-/+oossssoo+/-. 

Check Version of Ubuntu on Gnome Desktop using GUI

On Ubuntu 18.04 LTS the default desktop environment is Gnome but the previous version of Ubuntu was using Unity desktop environment.

First, Open the system settings windows as shown in the following image and click on the settings icon which first out of three icons at the bottom of the popup box.

You will get system information as below.

How to check Ubuntu Version - check version with GUI
How to check Ubuntu Version – check version with GUI

Conclusion

You have successfully learned how to check ubuntu version using different methods. If you have any queries regarding this please don’t forget to comment below.

Install Composer on Ubuntu

Composer is one the best dependency management tool for PHP which can install and update project dependency seamlessly. When installing a package it also checks on which another package current package depends on and then it installs all the dependencies. In this tutorial, you are going to learn how to Composer on Ubuntu 18.04.

Prerequisites

Before you start to install Composer on Ubuntu 18.04. You must have the non-root user account on your server/desktop with sudo privileges.

1. Install Composer

Before you install Composer you should update apt package manager index by typing.

sudo apt update

Now you should install some dependencies for Composer, you can do so by typing

sudo apt install curl php-cli php-mbstring git unzip

Enter following commands to download Composer Setup.

cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php

Now, you should verify that the installer matches the SHA-384 hash for the data integrity of latest installer found on the Composer Public Keys or Signatures page by running the following command.

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

Check if installation script is corrupted by

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You should get following output:

Output
Installer Verified

If you dont see above output you may get Installer Corrupt output. If this case download composer again and check hash value until you get Installer Verified output.

Now run following command to install Composer globally inside /usr/local/bin directory.

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You should get the following output

Output
Output
All settings correct for using Composer
Downloading...

Composer (version 1.6.3) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Run following command to confirm the installation

composer

You will see following output

Output
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer 1.6.3 2018-01-31 16:28:17

Usage:
  command [options][arguments]Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

If you see above output, you have installed composer successfully.

Use Composer In PHP Project

Now Composer is globally installed on your system. To use composer you should have a root directory for your project inside that directory you will install the dependency package with Composer.

Create directory NewProject as a root directory of your project.

sudo mkdir NewProject
cd NewProject

Now install latest version of guzzlehttp/guzzle package by using following command.

composer require guzzlehttp/guzzle

After installing package you will see that Composer created three files composer.json file,composer.lock file which contains package names with version, and vendor directory.
Enter the following command to check.

ls -l
Output
Output
total 12
-rw-rw-r-- 1 linux4one admin   59 Nov 11 20:13 composer.json
-rw-rw-r-- 1 linux4one admin 2934 Nov 11 20:13 composer.lock
drwxrwxr-x 4 linux4one admin 4096 Nov 11 20:13 vendor

Now, you have installed package guzzle, create a file test.php and copy following code inside that file. It will check statuscode for a URL if it loads successfully it will give 200 otherwise different
number.

test.php
<?php

require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;

$client = new Client(); 
$res = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle'); 
echo "statuscode : ".$res->getStatusCode();

Run above script by typing

php test.php

Output should be

statuscode : 200

If you want to update package then you can use following command.

composer update

Conclusion

You have successfully learned how to install Composer on Ubuntu 18.04. If you have any queries regarding this dont forget to comment bellow.

Install LAMP stack on Ubuntu

LAMP stack is a set of packages such as Linux, Apache, MySQL, and PHP. LAMP stack is used for hosting Websites and Apps. In this tutorial, you are going to learn How to install LAMP stack on Ubuntu.

Prerequisites

Before you start to install LAMP stack on Ubuntu 18.04. You must have the non-root user account on your server/desktop with sudo privileges.

1. Install Apache

First, you will need to update the package manager index by typing

sudo apt update

Now install Apache by running following command

sudo apt install apache2

Confirm the installation of Apache by using the following command

sudo systemctl status apache2

If you have UFW firewall enabled then you should open port 80 for HTTP and 443 for HTTPS to Apache Web Server. Enter the following commands to enable ports.

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

Now you can verify your Apache installation by visiting http://YOUR_DOMAIN_NAME/ or http://localhost/

2. Install MariaDB

Update apt package manager index by typing following command

sudo apt update

Now you can install MariaDB by running following command

sudo apt install mariadb-server mariadb-client

Check the status and confirm installation by typing following command

sudo systemctl status apache2

To secure MariaDB run following command.

sudo mysql_secure_installation

Once you execute above command you will be asked to enter current password (Press ENTER for none) then you will be asked following questions, enter y for following questions:

  • Set root password? : y
  • Remove anonymous users? : y
  • Disallow root login remotely? : y
  • Remove test database and access to it? : y
  • Reload privilege tables now? : y

3. Install PHP

To install PHP you should first update package manager index then you will install PHP.

Update apt package manager index by typing

sudo apt update

Install PHP and some of its common extensions by typing

sudo apt install php php-common php-mysql php-gd php-cli

After installing PHP you can confirm installtion by creating info.php file. Run following command to create info.php file.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now visit http://YOUR_DOMAIN_NAME/info.php or http://localhost/info.php in your browser to confirm installtion.

Conclusion

You have successfully learned how to install LAMP stack on Ubuntu 18.04. If you have any queries regarding this please don’t forget to comment below.