How to install multiple or two versions of Terraform? - terraform

I have a lot of Terraform modules written in Terraform 0.11 using gcp-provider of Terraform and want to upgrade the same to Terraform 0.12.
For this purpose, I need to keep both the versions installed on my system and use the version according to the version the module is written in.
I will go one by one in every module and upgrade the module using terraform 0.12upgrade to be compatible with Terraform 0.12 as per this documentation.
How to safely keep two versions of Terraform in one System?

I use Ubuntu 18.04 and I achieved this safely following the below steps. Similar steps can be followed to do the same on any Linux distro (making sure you are downloading the compatible binary. Confirm here)
NOTE Running the following commands as root or sudo user
Create directories to keep the Terraform binaries
$ mkdir -p /usr/local/tf
$ mkdir -p /usr/local/tf/11
$ mkdir -p /usr/local/tf/12
Download the binaries for both the versions
Download and extract the binary for Terraform 0.11 in a separate directory:
$ cd /usr/local/tf/11
$ wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip
$ unzip terraform_0.11.14_linux_amd64.zip
$ rm terraform_0.11.14_linux_amd64.zip
Download and extract the binary for Terraform 0.12 in a separate directory:
$ cd /usr/local/tf/12
$ wget https://releases.hashicorp.com/terraform/0.12.20/terraform_0.12.20_linux_amd64.zip
$ unzip terraform_0.12.20_linux_amd64.zip
$ rm terraform_0.12.20_linux_amd64.zip
Create symlinks for both the Terraform versions in /usr/bin/ directory:
ln -s /usr/local/tf/11/terraform /usr/bin/terraform11
ln -s /usr/local/tf/12/terraform /usr/bin/terraform12
# Make both the symlinks executable
chmod ugo+x /usr/bin/terraform*
Calling different versions
Now, command terraform11 invokes version 0.11 and terraform12 invokes version 0.12
Example:
$ terraform11
$ terraform12
NOTE
Keeping the binaries in separate directories helps to separate their plugins as well without disturbing each other.

I'd highly recommend the tfenv tool. It sanely and easily can be used to manage multiple terraform installations. It's familiar if you've ever used nvm (for nodejs) or rvm (for ruby).
You can even add a .terraform-version file to your modules and the tool will automatically switch terraform versions for you when you cd into a module.

Make your life easy and install tfswitch. It takes care of installing and switching between versions you need; and it works like a magic.

If using multiple environments with terraform, tfenv would be the best tool to use. I initially had the same error and was able to run tfenv use version-number and it solved my issue. For background, I use terraform for personal and work projects on the same computer.

Related

How to uninstall terraform and install newer version of terraform on windows 10 linux subsystem?

i want to install a newer version of terrafrom 0.12.24 on windows 10 linux subsystem. i am new to using this interface and i am not sure how to do it
i installed terraform on Windows 10 linux subsystem using these commands
wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip -O
terraform.zip;
unzip terraform.zip;
sudo mv terraform /usr/local/bin;
rm terraform.zip;
however this installed older version.. and when i ran some script it gave me this error.
Failed to load root config module: Error parsing terraform/local/terraform.tf: At 119:12: Unknown token: 119:12 IDENT file
To fix this i am planning to upgrade to newer version of terrafrom 0.12.24. could someone provide me with commands to uninstall and install newer version of terraform thanks.
This should install 0.12.24 version
wget https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip
unzip terraform_0.12.24_linux_amd64.zip
sudo mv terraform_0.12.24_linux_amd64 /usr/local/bin
rm terraform_0.12.24_linux_amd64.zip
Let me explain what these steps are:
1) The link given along with wget is the downloadable link for the required package. wget helps in downloading it. I changed the version here as your required version.
2) unzip it
3) moved to /usr/local/bin folder.
4) Removing the zip as we have already copied the contents to bin folder
This is probably more appropriate as a comment but I do not yet have the reputation to do that. As of version 0.15.1, it unzipped only as "terraform", so line 3 in the above answer could just be:
sudo mv terraform /usr/local/bin
And this worked for me.

Upgrade Terraform version

I'm start using Terraform and I'm following the project updates on his Github repository.
I see new releases are available on releases section:
https://github.com/hashicorp/terraform/releases
I installed Terraform tool following the steps:
https://www.terraform.io/intro/getting-started/install.html
The new releases are a zip file with base code but I want to know how I can install it on my computer (OSX). What I downloaded when I installed for the first time was a zip file with just a "terraform" file as unix executable.
How I can generate this Unix executable from the zip available on the github releases section?
Any idea?
Thank you!
If you use Homebrew on MacOS already, you can install Terraform simply by
$ brew install terraform
and upgrade by
$ brew upgrade terraform
In fact, you might be interested in letting Homebrew also control other tools:
$ brew install awscli
$ brew install packer
$ brew cask install docker
$ brew cask install virtualbox
$ brew cask install vagrant
There are packages for each OS available on the Downloads Page.
Pick the appropriate package for your OS and download the zip file.
Extract the contents of the zip file, which should be a single terraform binary, into some reasonable location that's in your PATH (e.g. /usr/local/bin).
Add execute permissions: e.g. chmod u+x /usr/local/bin/terraform.
Run terraform and make sure you see the help text.
You could download the binary :
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip
then :
unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
then :
mv terraform /usr/local/bin/
make sure to change ${TERRAFORM_VERSION} by the version you want to install
exemple :
0.13.2
**TLDR:
for Installation - brew install terraform
for Upgrade - brew upgrade terraform
for Verification - terraform --version
i'm new to using terraform, but to use terraform with multiple versions i use tfenv as terraform version manager
tfenv command
$ tfenv
tfenv 2.2.3
Usage: tfenv <command> [<options>]
Commands:
install Install a specific version of Terraform
use Switch a version to use
uninstall Uninstall a specific version of Terraform
list List all installed versions
list-remote List all installable versions
version-name Print current version
init Update environment to use tfenv correctly.
pin Write the current active version to ./.terraform-version
terraform currently i use
$ terraform -version
Terraform v1.1.9
on linux_amd64
list of terraform i have
$ tfenv list
1.2.0-rc2
* 1.1.9 (set by /home/takimi/.tfenv/version)
change terraform version
$ tfenv use 1.2.0-rc2
and if you want to use another terraform version you just install it with command tfenv install <terraform version>
Adding this response for the sake of completeness.
Hashicorp came up with their own brew taps for all Hashicorp products for MacOS platforms.
to upgrade ...
brew upgrade hashicorp/tap/{vault|consul|nomad|terraform|packer}
to install ...
brew install hashicorp/tap/{vault|consul|nomad|terraform|packer}
Reference : https://www.hashicorp.com/blog/announcing-hashicorp-homebrew-tap
This is detailed in the repository README under 'Developing Terraform' HERE
Essentially...
Ensure you've installed the GO programming language (version 1.7+ at the time of writing).
Create a GO workspace directory.
Create the $GOPATH environment variable pointing to the GO workspace directory you just created.
Add $GOPATH/bin to your $PATH
Clone the Terraform repo (or extract zip) to $GOPATH/src/github.com/hashicorp/terraform
Run make dev within the Terraform repo.
The Terraform binary should be created in $GOPATH/bin
I would recommend you follow the steps in the README as opposed to what I've written above it's comprehensive and may change.
You shouldn't need to do any of this unless your actively developing Terraform, otherwise stick with the installation method in the getting started guide.
To work with multiple Terraform versions, I'm using tfswitch. This allows you to switch between the terraform versions which makes things much easier.
You can find the documentation here.
I was able to use this tool to manage terraform versions: https://github.com/Zordrak/tfenv. The first step was to remove the existing installation with: rm '/usr/local/bin/terraform'
You could use something like asdf or tfenv to manage the terraform binaries.
Gives you the advantage of having the option to use different version per project if required.
$sudo apt-get upgrade
it will work in linux if you have setup right path for terraform

How to install packages in Linux (CentOS) without root user with automatic dependency handling?

Is it possible to use RPM or YUM or any other package manager in Linux, specifically CentOS, to install a package either already downloaded or from repo to a custom location without admin/root access?
I tried building from sources, using cmake, configure, make, make install etc, but, it ended up having so many dependencies one after other.
Or are there any better alternatives?
It is possible to use yum and rpm to install any package in the repository of the distribution. Here is the recipe:
Find the package name
Use yum search.
Download
Download the package and all of its dependencies using yumdownloader (which is available on CentOS by default). You'll need to pass it --resolve to get dependency resolution. yumdownloader downloads to the current directory unless you specify a --destdir.
mkdir -p ~/rpm
yumdownloader --destdir ~/rpm --resolve vim-common
Choose a prefix location
It might be ~, ~/centos, or ~/y. If your home is slow because it is on a network file system, you can put it in /var/tmp/....
mkdir ~/centos
Extract all .rpm packages
Extract all .rpm packages to your chosen prefix location.
cd ~/centos && rpm2cpio ~/rpm/x.rpm | cpio -id
rpm2cpio outputs the .rpm file as a .cpio archive on stdout.
cpio reads it from from stdin
-i means extract (to the current directory)
-d means create missing directory
You can optionally use -v: verbose
Configure the environment
You will need to configure the environment variable PATH and LD_LIBRARY_PATH for the installed packages to work correctly. Here is the corresponding sample from my ~/.bashrc:
export PATH="$HOME/centos/usr/sbin:$HOME/centos/usr/bin:$HOME/centos/bin:$PATH"
export MANPATH="$HOME/centos/usr/share/man:$MANPATH"
L='/lib:/lib64:/usr/lib:/usr/lib64'
export LD_LIBRARY_PATH="$HOME/centos/usr/lib:$HOME/centos/usr/lib64:$L"
Edited note (thanks to #AmitNaidu for pointing out my mistake):
According to bash documentation about startup files, when connecting to a server via ssh, only .bashrc is sourced:
Invoked by remote shell daemon
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If Bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc, if that file exists and is readable.
Now if you want to install a lot of packages that way, you might want to automate the process. If so, have a look at this repository.
Extra note: if you are trying to install any of gcc, zlib, make, cmake, git, fish, zsh or tmux , you should really consider using conda, see my other answer.
TL;DR Use Miniconda, conda-forge is amazing.
curl "https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh" | sh
Or, alternatively:
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh > Miniconda.sh
bash Miniconda.sh -b -p ~/conda
# -b is used to specify that this is done "in batch", so skip the EULA prompt
# -p lets you specify where you want conda installed
Commonly wanted packages:
gcc conda install gcc
zlib conda install zlib
make conda install make
cmake conda install cmake
git conda install git
fish conda install -c conda-forge fish
zsh conda install -c ActivisionGameScience zsh
tmux conda install -c conda-forge tmux
This tmux has a bug with the name of the ncurse library it uses. You can work around it by going to your da/lib folder and symlinking ln -sT libtinfow.so.6.1 libtinfo.so.6
For the rest, you can try https://anaconda.org/search?q=.
I've tried for a long time to get a package manager to work well on CentOS/RedHat but without success. The best I could do was to install a Gentoo Prefix at the correct location on another CentOS with root access, then scp a .tar.xz of the whole installation to the target server (only way to get a proper gcc for Gentoo Prefix). I could emerge (build & install) packages on the target server but kept hitting problems with locals and permissions.
I recently achieved a user installation of some interesting packages using conda. Here is how to install it from the command line:
curl "https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh" | sh
If like me, your home folder is hosted on a remote drive (a network file system), you might not want to install it in your home folder, so you might want to use something like mkdir /var/tmp/lo then specify an installation folder like /var/tmp/lo/da during the installation.
You'll then be able to install quite a lot of packages, though maybe not all those you wanted. Most of the time, if it is not in the default channel, it will be in conda-forge. You can check for existing packages at https://anaconda.org/search?q=
Other package managers I've tried to use after conda:
Linuxbrew
I thought that with that it would be easy to install homebrew (linuxbrew) but their sources are messy and use hard-coded absolute path to ruby interpreter, which fails because it isn't the last version and so on and so on and I gave up.
Nix
Nix still requires you to use the /nix folder. They hard-coded it too and it's hard to sed it correctly from every download it has to do during the installation (let alone updates).
Gentoo Prefix
I expect Gentoo Prefix to be easier to install directly now that we gcc can be used on the target server. -- Ok, I tried but met permissions bugs during installation (2018-09-28):
portage.exception.OperationNotPermitted: chown(b'~/gentoo/tmp/var/tmp/portage/sys-apps/gentoo-functions-0.12/image/var', 2000, 2000)
PkgSrc
I'm going to try pkgsrc now. -- Use (older) version 64-bit EL 6.x if on CentOS 6 or if encountering (G)LibC version issues with the 7.x one. -- No luck, pkgsrc hard codes /usr/pkg/sbin and /usr/pkg/bin. So it can't be used as user, unless maybe setting up a fakechroot environment. But I've never done that and I expect usability issues.
Please comment/answer if you succeed in installing any other package manager.
Download the packages, and indicate to include dependencies with the --resolve flag.
yumdownloader --resolve openslide-tools
Iterate over all downloaded rpm files.
for i in *.rpm; do rpm2cpio $i | cpio -idv; done
the output will be stored in your present working directory $PWD/usr/*
This answer by goldilocks sounds like what you are looking for.
https://unix.stackexchange.com/a/61295
It's still not a pretty process, but seems easier than building from source.
Otherwise you might want to look into non-root package managers as an alternative to yum.
Yes it is. If the software is packaged in repos. And admin installed
PackageKit-command-not-found package.
See:
https://fedoraproject.org/wiki/Features/PackageKitCommandNotFound

Get the Perl rename utility instead of the built-in rename

Many sites (including various SO articles) talk about using "rename" using Perl expressions to rename files.
This would be perfect, but apparently this is not the rename utility I have, and none of these articles seem to comprehend that there are multiple versions of "rename" and I can't seem to find where to get version that accepts Perl expressions.
How can I get my hands on the more powerful rename utility mentioned here, here, and here?
I'm running Fedora 20. My current rename command is from the util-linux package and apparently I need the Perl version, which is better.
I can only speak for Debian. The two programs are called
/usr/bin/rename.ul from the util-linux package (hence the .ul suffix)
/usr/bin/prename from the perl package
The actual rename command works via the /etc/alternatives mechanism, whereby
/usr/bin/rename is a symlink to /etc/alternatives/rename
/etc/alternatives/rename is a symlink to /usr/bin/prename
The same problem has been bugging me on Cygwin, which is a Red Hat product, so should be more similar to Fedora. I'll have a look on my company laptop on Monday. And I remember the Perl-rename having worked there sometimes. Probably before I installed util-linux.
If you install the Perl-rename to /usr/local/bin it will have precedence over rename from util-linux. Same goes for the manpage when installed to /usr/local/share/man/man1/.
I've just created a separate Perl-rename package on Github: https://github.com/subogero/rename
You can install it using cpan, which is the perl repository similar to pip for python.
Here is a tutorial on using cpan.
If you try to run rename it it looks like this
rename --help
call: rename from to files...
To install the perl rename you can do the following. You might need to install a few dependencies, you can generally just push enter
cpan
cpan1> install File::Rename
CPAN: Storable loaded ok (v2.20)
Going to read '/root/.cpan/Metadata'
Database was generated on Wed, 30 Sep 2015 08:17:02 GMT
Running install for module 'File::Rename'
....
Running Build install
Installing /usr/local/share/man/man1/rename.1
Installing /usr/local/share/perl5/File/Rename.pm
Installing /usr/local/share/man/man3/File::Rename.3pm
Installing /usr/local/bin/rename
Writing /usr/local/lib64/perl5/auto/File/Rename/.packlist
RMBARKER/File-Rename-0.20.tar.gz
./Build install -- OK
That is how you would install the rename from cpan.
Next is to get it working on your system. As you might have more then one rename installed.
which rename
/usr/bin/rename
When you actually want this one.
/usr/local/bin/rename --help
Usage:
rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E *perlexpr*]*|*perlexpr*
[ *files* ]
Options:
-v, -verbose
Verbose: print names of files successfully renamed.
-n, -nono
No action: print names of files to be renamed, but don't rename.
-f, -force
Over write: allow existing files to be over-written.
-h, -help
Help: print SYNOPSIS and OPTIONS.
-m, -man
Manual: print manual page.
-V, -version
Version: show version number.
-e Expression: code to act on files name.
May be repeated to build up code (like "perl -e"). If no -e, the
first argument is used as code.
-E Statement: code to act on files name, as -e but terminated by
';'.
I just put it into /usr/bin/ but with a slight different name to make sure I did not break any existing scripts / programs the depend on the old one.
ln -s /usr/local/bin/rename /usr/bin/rename.pl
I had to do the following:
# In bash
sudo yum install perl-CPAN
sudo cpan
# In CPAN shell
install Module::Build
install File::Rename
On RedHat 8.4
sudo yum install perl-CPAN
sudo cpan
install module::Build
install File::Rename
than you can create an alias:
alias prename='/usr/local/bin/rename'
an use:
touch pic.jpeg
prename 's/\.jpeg$/.jpg/' *.jpeg
For Debian-family (.deb) distros, I recommend #SzG's answer.
For RedHat-family (.rpm) distros (e.g. Fedora), if your time is precious (like mine), you can download, compile, and install, from source via cpan in one, terse command:
# Install (replace `rename-1.9` below with another version if desired)
curl -L "http://search.cpan.org/CPAN/authors/id/P/PE/PEDERST/rename-1.9.tar.gz" | tar -xz && ( cd "rename-1.9"; perl "Makefile.PL"; make && make install )
# Cleanup
rm -rf "rename-1.9"
Note:
INSTALL_BASE can be set to modify the base installation directory.
e.g. perl "Makefile.PL" INSTALL_BASE=/usr/local
source
For Arch Linux, its
sudo pacman -S perl-rename
I created a post about Perl's rename for many distro:
https://unix.stackexchange.com/a/727288/12574
rpm based distros:
dnf install prename
archlinux:
pacman -S perl-rename
*BSD:
pkg install p5-File-Rename
Debian like/Ubuntu
apt install rename
slackware:
slackbuild
I recently had to install the glorious Perl rename package to Alpine Linux in a Docker container for a Gitlab CI/CD operation:
apk update
apk add --no-cache make perl-utils
cpan File::Rename

when using CPAN in linux ubuntu should I run it using sudo / as root or as my default user

I get errors like this
Running make install
Prepending blib/arch and blib/lib of 17 build dirs to PERL5LIB; for 'install'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ERROR: Can't create '/usr/local/man/man3'
Do not have write permissions on '/usr/local/man/man3'
You may have to su to root to install the package
(Or you may want to run something like
o conf make_install_make_command 'sudo make'
Will I still be able to run the software / library's that perl / CPAN installs as a my default user.
What is the best practice when working with CPAN / Perl on Debian like systems.
You should run the cpan command as your normal user. You have two choices:
Install modules into a directory under your home dir. local::lib will help you set that up.
Configure cpan to use sudo during the install phase. You do that by starting the cpan shell and typing:
o conf make_install_make_command 'sudo make'
o conf mbuild_install_build_command 'sudo ./Build'
o conf commit
The first line configures MakeMaker to use sudo. The second line does the same for Module::Build. The third line saves the changes.
If you want to install your modules for your own use, then you should be running it as yourself. Use local::lib to set up your environment variables so that this works nicely. You may wish to look at cpan minus as an alternative to the default cpan installer.
You may also wish to consider using perlbrew to install a newer version of perl that is completely independent from the system perl.
If you want to install them system wide, then I recommend (since you have tagged this ubuntu) looking at dh-make-perl to produce .deb files that you can install and uninstall with dpkg.
If you want to install modules as a non-root user, you can configure your cpan to use an install base:
makepl_arg [INSTALL_BASE=/home/nelaar/perl]
mbuildpl_arg [--install_base=/home/nelaar/perl]
And set PERL5LIB accordingly to /home/nelaar/perl/lib/perl5.

Resources